package com.sgcc.sx.pmos.common.utils;

import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ruolanmingyue on 2017/10/26.
 *
 * @function 用于过滤输入      防止输入大于500元还有就是限制小数点之后两位
 */

public class LengthFilter implements InputFilter {

    /**
     * 最大数字
     */
    public String MAX_VALUE = "";

    private EditText editText;
    /**
     * 小数点后的数字的位数
     */
    public int POINTER_LENGTH = 2;

    private static final String POINTER = ".";

    Pattern p;
    LengthTextWatcher lengthTextWatcher;

    public LengthFilter(EditText editText, String maxValue, int pointLength, boolean isadd) {
        this.editText = editText;
        this.MAX_VALUE = maxValue;
        this.POINTER_LENGTH = pointLength;
        //用于匹配输入的是0-9  .  这几个数字和字符
        p = Pattern.compile("([0-9]|\\.)*");
        if (isadd) {
            if (lengthTextWatcher == null) {
                lengthTextWatcher = new LengthTextWatcher(editText);
            }
            editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        editText.addTextChangedListener(lengthTextWatcher);
                    } else {
                        editText.removeTextChangedListener(lengthTextWatcher);
                    }
                }
            });
        }
    }

    public LengthFilter(EditText editText, String maxValue, int pointLength) {
        this.editText = editText;
        this.MAX_VALUE = maxValue;
        this.POINTER_LENGTH = pointLength;
        //用于匹配输入的是0-9  .  这几个数字和字符
        p = Pattern.compile("([0-9]|\\.)*");
        if (lengthTextWatcher == null) {
            lengthTextWatcher = new LengthTextWatcher(editText);
        }
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    editText.addTextChangedListener(lengthTextWatcher);
                } else {
                    editText.removeTextChangedListener(lengthTextWatcher);
                }
            }
        });
    }

    /**
     * source    新输入的字符串
     * start    新输入的字符串起始下标，一般为0
     * end    新输入的字符串终点下标，一般为source长度-1
     * dest    输入之前文本框内容
     * dstart    原内容起始坐标，一般为0
     * dend    原内容终点坐标，一般为dest长度-1
     */

    @Override
    public CharSequence filter(CharSequence source, int start, int end,
                               Spanned dest, int dstart, int dend) {

        String sourceText = source.toString();
        String destText = dest.toString();
//验证删除等按键
        if (TextUtils.isEmpty(sourceText)) {
            if (dstart == 0 && destText.indexOf(POINTER) == 1) {//保证小数点不在第一个位置
                return "0";
            }
            String deletetext = destText.substring(dstart, dend);
            if (deletetext.equals(POINTER)) {
                String first = destText.substring(0, dstart);

                String second = destText.substring(dend, destText.length());
                String sum = first + sourceText + second;
                BigDecimal a = new BigDecimal(sum);
                BigDecimal b = new BigDecimal(MAX_VALUE);
                //这里得到输入完之后需要计算的金额  如果这个金额大于了事先设定的金额,那么久直接返回  不需要加入输入的字符
                if (a.compareTo(b) > -1) {
                    return dest.subSequence(dstart, dend);
                }
            }
            return "";
        }
        Matcher matcher = p.matcher(source);
        //已经输入小数点的情况下，只能输入数字
        if (destText.contains(POINTER)) {
            if (!matcher.matches()) {
                return "";
            } else {
                if (POINTER.equals(source)) { //只能输入一个小数点
                    return "";
                }
            }
            //验证小数点精度，保证小数点后只能输入两位
            int index = destText.indexOf(POINTER);
            int length = destText.trim().length() - index;
            if (length > POINTER_LENGTH && dstart > index) {
                return "";
            }
        } else {
            //没有输入小数点的情况下，只能输入小数点和数字，但首位不能输入小数点和0
            if (!matcher.matches()) {
                return "";
            } else {
                if (POINTER_LENGTH == 0 && (dstart == 0 || destText.length() == dstart)) {
                    if ((POINTER.equals(source))) {
                        return "";
                    }
                } else if ((POINTER.equals(source)) && (destText.length() - dstart) > POINTER_LENGTH) {
                    return "";
                } else if ((POINTER.equals(source)) && dstart == 0) {//第一个位置输入小数点的情况
                    return "0.";
                }
            }
        }
        String first = destText.substring(0, dstart);

        String second = destText.substring(dstart, destText.length());
        String sum = first + sourceText + second;
        BigDecimal a = new BigDecimal(sum);
        BigDecimal b = new BigDecimal(MAX_VALUE);
        //这里得到输入完之后需要计算的金额  如果这个金额大于了事先设定的金额,那么久直接返回  不需要加入输入的字符
        if (a.compareTo(b) > -1) {
            return dest.subSequence(dstart, dend);
        }
        //如果输入的金额小于事先规定的金额
        return dest.subSequence(dstart, dend) + sourceText;
    }

}